package com.foreverwin.jzymes.report.crop.service.impl;


import com.foreverwin.jzymes.common.service.IProductionShiftService;
import com.foreverwin.jzymes.common.service.IResourceAdviceService;
import com.foreverwin.jzymes.common.service.ISiteAdviceService;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.report.crop.mapper.CropMobilityReportMapper;
import com.foreverwin.jzymes.report.crop.mapper.FailureRateReportMapper;
import com.foreverwin.jzymes.report.crop.service.ICropMobilityReportService;
import com.foreverwin.jzymes.report.crop.service.IFailureRateReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class FailureRateReportServiceImpl implements IFailureRateReportService {

    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    FailureRateReportMapper failureRateReportMapper;
    @Autowired
    IProductionShiftService productionShiftService;

    private static final long TIME = 12 * 60l;
    private static final long ONE_DAY = 1000 * 60 * 60 * 24l;

    @Override
    public Map<String, Object> findFailureRateReport(Map<String, Object> jsonMap) {

        Assert.notNull( jsonMap.get( "startDate" ) , I18nUtil.getI18nText("report.rate.startDate"));
        Assert.notNull( jsonMap.get( "endDate" ) , I18nUtil.getI18nText("report.rate.endDate"));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String cncType = (String)jsonMap.get("cncType");
        String resrce = (String)jsonMap.get( "resrce" );
        String site = CommonMethods.getSite();
        Date start = null;
        Date end = null;
        try {
            start = simpleDateFormat.parse( (String) jsonMap.get( "startDate" ) );
            end = simpleDateFormat.parse( (String) jsonMap.get( "endDate" ) );
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long startL = start.getTime();
        long endL = end.getTime();
        Long time = startL;
        List<Map<String,Object>> shiftList = productionShiftService.findShiftList( site );
        List<Map<String,Object>> rtnList = new ArrayList<>();
        BigDecimal total = BigDecimal.ZERO;
        BigDecimal timeCount =  BigDecimal.ZERO;
        BigDecimal C = BigDecimal.ZERO;
        BigDecimal D = BigDecimal.ZERO;
        DecimalFormat percent = new DecimalFormat("0.00%");

        while (time <= endL) {
            Date date = new Date(time);
            String year = simpleDateFormat.format( date );
            if ( StringUtils.isEmpty( resrce ) ){
                List< Map<String,Object> > resrcelist = resourceAdviceService.findResrceBySite( site );
                for ( Map<String,Object> Map : resrcelist ){
                    resrce = (String) Map.get("RESRCE");
                    for ( Map<String,Object> map : shiftList ){
                        Map<String,Object> rtnMap = new HashMap<>();
                        rtnMap.put( "time" , year );
                        rtnMap.put( "cncType" , cncType );
                        String shift = (String)map.get("PRODUCTION_SHIFT");
                        Date[] dates = productionShiftService.findShiftRange( site , shift, date );
                        Long cropD = failureRateReportMapper.selectFailureRateByReport( site , resrce ,dates[0],dates[1] );
                        Long count = failureRateReportMapper.selectFailureRateCount( site ,resrce, dates[0],dates[1] );
                        if ( cropD != null ){
                            C = new BigDecimal( cropD );
                        }
                        if ( count != null){
                            D = new BigDecimal( count );
                        }
                        total = total.add( C );
                        timeCount = timeCount.add( D );
                        //BigDecimal D = new BigDecimal( TIME );
                        BigDecimal crop = BigDecimal.ZERO;
                        if ( D != BigDecimal.ZERO ){
                            crop = C.divide( D , 4, RoundingMode.HALF_UP );
                        }
                        //BigDecimal crop = C.divide( Count , 4, RoundingMode.HALF_UP );
                        rtnMap.put( "resource" , resrce );
                        rtnMap.put( "shift" , shift );
                        rtnMap.put( "downTime" , C );
                        rtnMap.put( "crop" ,  percent.format( crop ) );
                        rtnList.add( rtnMap );
                    }

                }

            }else {
                for ( Map<String,Object> map : shiftList ){
                    Map<String,Object> rtnMap = new HashMap<>();
                    rtnMap.put( "time" , year );
                    rtnMap.put( "cncType" , cncType );
                    String shift = (String)map.get("PRODUCTION_SHIFT");
                    Date[] dates = productionShiftService.findShiftRange( site , shift, date );
                    Long cropD = failureRateReportMapper.selectFailureRateByReport( site , resrce ,dates[0],dates[1] );
                    Long count = failureRateReportMapper.selectFailureRateCount( site ,resrce, dates[0],dates[1] );
                    if ( cropD != null ){
                        C = new BigDecimal( cropD );
                    }
                    total = total.add( C );
                    if ( count != null ){
                        D = new BigDecimal( count );
                    }
                    timeCount = timeCount.add( D );
                    BigDecimal crop = BigDecimal.ZERO;
                    if ( D != BigDecimal.ZERO ){
                        crop = C.divide( D , 4, RoundingMode.HALF_UP );
                    }
                    rtnMap.put( "resource" , resrce );
                    rtnMap.put( "shift" , shift );
                    rtnMap.put( "downTime" , C );
                    rtnMap.put( "crop" ,  percent.format( crop ) );
                    rtnList.add( rtnMap );
                }
            }
            time += ONE_DAY;
        }
        Map rturn = new HashMap( );
        //BigDecimal inT = new BigDecimal( rtnList.size() * TIME );
        BigDecimal sum = BigDecimal.ZERO;
        if ( timeCount != BigDecimal.ZERO ){
            sum = total.divide( timeCount ,4, RoundingMode.HALF_UP );
        }
        rturn.put("total", percent.format( sum ) );
        rturn.put("list", rtnList);

        return rturn;
    }

}
